package com.demo.search;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.demo.search.util.AnalyzerUtils;

/**
 * 观察分析器处理
 * {@link IKAnalyzer}为中文分析器(分词器)
 * @author ryuu.kk
 *
 */
public class AnalyzerDemo {

	private static final String[] examples = {
		"The quick brown fox jumped over the lazy dog",
		"XY&Z Corporation - xyz@example.com",
		"这是一段中文,用来测试对中文的语法分析"
	};
	
	private static final Analyzer[] analyzers = new Analyzer[] {
		new WhitespaceAnalyzer(Version.LUCENE_36),
		new SimpleAnalyzer(Version.LUCENE_36),
		new StopAnalyzer(Version.LUCENE_36),
		new StandardAnalyzer(Version.LUCENE_36),
		new IKAnalyzer()
	};
	
	public static void main(String[] args) throws IOException {
		String[] strings = examples;
		
		if (args.length > 0) {
			strings = args;
		}
		
		for (String text : strings) {
			analyze(text);
		}
		
	}
	
	private static void analyze(String text) throws IOException {
		System.out.println("Analyzing \"" + text + "\"");
		for (Analyzer analyzer : analyzers) {
			
			String name = analyzer.getClass().getSimpleName();
			System.out.println("  " + name + ":");
			
			AnalyzerUtils.displayTokens(analyzer, text);
			System.out.print("\n");
		}
	}
}
